package com.shuang.tree19;


import java.util.LinkedList;
import java.util.Queue;

public class MinDepth {

    //左右孩子都为空的时候，才说明遍历到最低点了
    public int minDepth(TreeNode root) {

        if (root == null) {
            return 0;
        }

        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);

        int depth = 0;

        while (!queue.isEmpty()) {

            int size = queue.size();

            depth++;

            //用于记录每次循环弹出的节点
            TreeNode cur = null;

            for (int i = 0; i < size; i++) {

                cur = queue.poll();

                //如果当前节点的左右孩子都为空，直接返回最小深度
                if (cur.left == null && cur.right == null) {
                    return depth;
                }

                if (cur.left != null)
                    queue.offer(cur.left);
                if (cur.right != null)
                    queue.offer(cur.right);
            }
        }

        return depth;
    }
}
